package com.sxit.tdf.server.config;

import cn.dev33.satoken.exception.DisableLoginException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.hutool.json.JSONObject;
import com.sxit.tdf.server.exception.TdfException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义全局异常处理类
 *
 * @author 橙子高
 * @date 2022/2/5 21:18
 * @describe
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
  @ResponseBody
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  @ExceptionHandler(Exception.class)
  public String exceptionHandler(Exception e) {
    JSONObject json = new JSONObject();
    // 处理后端验证失败产生的异常
    if (e instanceof MethodArgumentNotValidException) {
      MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
      json.set("success", false);
      json.set("errorMessage", exception.getBindingResult().getFieldError().getDefaultMessage());
      json.set("errorCode", 500);
    }
    // 处理业务异常
    else if (e instanceof TdfException) {
      System.out.println("------->");
      log.error("执行异常", e);
      TdfException exception = (TdfException) e;
      json.set("success", false);
      json.set("errorCode", 500);
      json.set("errorMessage", exception.getMsg());
    } else if (e instanceof NotLoginException) { // 如果是未登录异常
      NotLoginException ee = (NotLoginException) e;

      json.set("success", false);
      json.set("errorCode", 500);
      json.set("errorMessage", ee.getMessage());
    } else if (e instanceof NotRoleException) { // 如果是角色异常

      NotRoleException ee = (NotRoleException) e;
      json.set("success", false);
      json.set("errorCode", 500);
      json.set("errorMessage", "无此角色：" + ee.getMessage());

    } else if (e instanceof NotPermissionException) { // 如果是权限异常
      NotPermissionException ee = (NotPermissionException) e;
      json.set("errorMessage", "无此权限：" + ee.getMessage());
      json.set("errorCode", 500);

    } else if (e instanceof DisableLoginException) { // 如果是被封禁异常
      DisableLoginException ee = (DisableLoginException) e;
      json.set("errorMessage", "账号被封禁：" + ee.getMessage());
      json.set("errorCode", 500);

    }
    // 处理其余的异常
    else {
      json.set("success", false);
      json.set("errorCode", 500);
      log.error("执行异常", e);
      json.set("errorMessage", "执行异常");
    }
    return json.toString();
  }

  @ResponseBody
  @ResponseStatus(HttpStatus.UNAUTHORIZED)
  @ExceptionHandler(NotLoginException.class)
  public String unLoginHandler(Exception e) {
    JSONObject json = new JSONObject();
    json.set("error", e.getMessage());
    return json.toString();
  }
}
